package org.moflon.boardgamegui.controller;
 
import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EPackage;

import BoardGame.*;
import GameRules.*;

public class GenericControllerImpl extends GenericController<Board,Field,Figure> {

	public GenericControllerImpl(int width, int height) {
		super(width, height);
	}
	
	public static void main(String[] args) {
		new GenericControllerImpl(8, 8);
	}
	
	@Override
	public EPackage getEPackage() {
		return BoardGamePackage.eINSTANCE;
	}

	@Override
	public int getBoardWidth() {
		return getBoard().getWidth();
	}

	@Override
	public int getBoardHeight() {
		return getBoard().getHeight();
	}

	@Override
	public EList<Field> getFields() {
		return getBoard().getFields();
	}

	@Override
	public Figure getFigure(Field field) {
		return field.getFigure();
	}

	@Override
	public int getFieldXCoordinate(Field field) {
		return field.getX();
	}

	@Override
	public int getFieldYCoordinate(Field field) {
		return field.getY();
	}

	@Override
	public EFactory getEFactory() {
		return GameRulesFactory.eINSTANCE;
	}

	@Override
	public void addFigureToField(Figure figure, Field field) {
		field.setFigure(figure);	
	}

	@Override
	public List<EClass> getConcreteFigureClasses() {
		ArrayList<EClass> classes = new ArrayList<EClass>();
		for (EClassifier classifier : GameRulesPackage.eINSTANCE
				.getEClassifiers()) {
			if (classifier instanceof EClass
					&& BoardGamePackage.eINSTANCE.getFigure().isSuperTypeOf((EClass) classifier)
					&& !classifier.equals(BoardGamePackage.eINSTANCE.getFigure()))
				classes.add((EClass) classifier);
		}
		return classes;
	}

	@Override
	public Board makeNewBoard(int width, int height) {
		Board board= BoardGameFactory.eINSTANCE.createBoard();
		board.initialize(width, height);
		return board;
	}

	@Override
	public void fieldPressed(Field field) {
		getBoard().pressed(field);
		updateView();
	}
}
